package ex.datastream.windows.windowAssigners;

import ex.datastream.ApiFrame;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;

/**
 *Count Window是基于数量的窗口，根据固定的数量定义窗口的大小，例如每3条数据形成一个窗口
 */
public class CountWindows extends ApiFrame {
    public static void main(String[] args) throws Exception {
        CountWindows windows=new CountWindows();
        windows.getEnv();

        SingleOutputStreamOperator<Tuple2<String,Long>> outputStreamOperator =windows.getSingleOutputStreamOperator();
        KeyedStream<Tuple2<String,Long>, String> keyedStream = outputStreamOperator.keyBy(value -> value.f0);
        //每3条数据形成一个窗口，进行求和打印
        keyedStream.countWindow(3).sum(1).print();
        //每1条数据时形成一个窗口,对最近2条求和打印
//        keyedStream.countWindow(2,1).sum(1).print();
        windows.env.execute();
    }
}
